package com.bolun998300.personal_blog_springboot.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.bolun998300.personal_blog_springboot.pojo.Blogger;
import com.bolun998300.personal_blog_springboot.service.BloggerService;

public class BlogRealm extends AuthorizingRealm {



	@Autowired
	private BloggerService bloggerService;
	
	//授权
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection token) {
		SimpleAuthorizationInfo s = new SimpleAuthorizationInfo();
        return s;
	}
	
	//认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		UsernamePasswordToken t = (UsernamePasswordToken) token;
		String bloggerName= token.getPrincipal().toString();
		Blogger blogger = bloggerService.getByName(bloggerName);
		String password = new String(t.getPassword());
		if(null == blogger ||  !blogger.getPassword().equals(password)) {
			throw new AuthenticationException();
		}
		
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(blogger, blogger.getPassword(),getName());
        return authenticationInfo;
	}

}
